import { Graphics } from "pixi.js";
import { useColorStore } from "../store/Color";
import { Tool } from "./Tool";
import { userInput } from "./User";
import { Matrix } from "game-algorithm";
import { useFloodFillWorker } from "../workers/useWorkers";
import { timeSplit } from "../utils/timeSplit";
/** 油漆桶 */
export class Paint extends Tool {
    name = "填充";
    sliceSize = 3000;
    ColorStore: ReturnType<typeof useColorStore>;
    drawStart = async ({ layer, data: { x, y } }: userInput) => {
        const { color, alpha } = this.ColorStore;
        const m = layer.tileMap.dataMatrix;
        const result = await useFloodFillWorker(m as any, x, y);
        timeSplit(
            result,
            ([x, y]) => {
                layer.drawPoint(x, y, color, alpha, 0);
            },
            this.sliceSize,
            100
        );
    };
    drawEnd({ layer, data: { x, y } }: userInput): void {}
    drawing({ layer, data: { x, y } }: userInput): void {}
    protected size = 10;
    /** 用于渲染画笔轨迹的转换数组 */
    private styleArray: number[][] = [];
    constructor() {
        super();
        this.ColorStore = useColorStore();
    }
    /** 每次转换工具的时候触发这个函数 */
    prepare() {
        this.refreshDrawRange();
    }
    refreshDrawRange() {
        this.styleArray = new Matrix({
            height: 10,
            width: 10,
            voidValue(x, y) {
                return [x, y];
            },
        }) as any as number[][];
    }
    blend: 0 | 1 = 0;
    /** 提供一个 Graphics 来绘制笔的样子 */
    drawStyle(g: Graphics) {
        this.refreshDrawRange();
        g.clear();
        const { alpha, color } = this.ColorStore;
        g.beginFill(color, alpha / 255);
        this.styleArray.forEach(([x, y]) => g.drawRect(x, y, 1, 1));
        g.endFill();
    }
    closed(): void {}
}
